import 'package:ceramics/util/ab_const_url.dart';
import 'package:ceramics/util/ab_log_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:webview_flutter/webview_flutter.dart';

class ABWebViewPage extends StatefulWidget {
  const ABWebViewPage({super.key,required this.url,required this.title});
  final String url;
  final String title;
  @override
  State<ABWebViewPage> createState() => _ABWebViewPageState();
}

class _ABWebViewPageState extends State<ABWebViewPage> {

  final _webController = WebViewController();

  @override
  void initState() {
    super.initState();
    if (widget.url == ABURLs.privacyPolicyURL) {
      LogPvManger.stat(page: 'terms');
    } else if (widget.url == ABURLs.termsOfServiceURL) {
      LogPvManger.stat(page: 'privacy');
    }

    // _changeBodyColor();
  }

  _changeBodyColor() async {
    _webController.setJavaScriptMode(JavaScriptMode.unrestricted);
    var delegate = NavigationDelegate(
      onPageFinished: (url) async {
        // await _webController.runJavaScript("document.body.style.backgroundColor = '#1D003B'");
        await _webController.runJavaScript("""
      document.body.style.fontSize = '40px';
    """);
      },
    );
    _webController.setNavigationDelegate(delegate);
    _webController.loadRequest(Uri.parse(widget.url));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(widget.title)),
      body: SafeArea(
        child: Container(
          width: double.infinity,
          height: double.infinity,
          // color: const Color(0xFF1D003B),
          child: InAppWebView(initialOptions: InAppWebViewGroupOptions(
            crossPlatform: InAppWebViewOptions(transparentBackground: true)
          ),initialUrlRequest: URLRequest(url: Uri.parse(widget.url))),
        ),
      )
    );
  }
}
